package com.tl.ip_location_relation_v4.service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import com.tl.ip_location_relation_v4.utils.IpUtil;
import com.tl.ip_location_relation_v4.utils.JDBCUtils;

public class IpSearchV3 {
	public static String search(String ip) {
		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		long longIp = IpUtil.ipToLong(ip);
		try {

			conn = JDBCUtils.getConnection();
			st = conn.createStatement();
			String sql = "select location from ip_location_data where "
					+ longIp + ">= start_ip and " + longIp + "<= end_ip";
			rs = st.executeQuery(sql);
			rs.next();
			return rs.getString("location");

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			JDBCUtils.release(conn, st);
		}

	}

}
